#include "UpdatePointCmd.h"

UpdatePointCmd::UpdatePointCmd(QObject *parent) :
    BaseCmd(parent)
{
    this->m_protocolType = ProtocolType::CloudProtocol;
    this->m_functionCode = FunctionCode;
    m_updatePointList.clear();
}

Package UpdatePointCmd::toPackage()
{
    Package package;
    QByteArray body;

    quint16 size = m_updatePointList.size();
    if(size != 0)
    {
        body.append(DataTypeConver::intToByteArray(size,2));

        for(int i=0; i<size; i++)
        {
            ControlPointStruct updatePoint = m_updatePointList.at(i);
            body.append(DataTypeConver::intToByteArray(updatePoint.address(),2));
            body.append(DataTypeConver::intToByteArray(updatePoint.value(),2));
        }
    }

   // body = encrypt(body,KeyType::AES);

    package.setId(m_cmdId);
    package.setFunction(FunctionCode);
    package.setBody(body);

    return package;
}

void UpdatePointCmd::unPackage(Package package)
{
    QByteArray body;
    quint16 index = 0;

   // body = decrypt(package.body(),KeyType::AES);

    m_cmdId = package.id();
    quint16 size = body.mid(index,2).toHex().toInt(0,16);
    index += 2;

    for(int i=0; i<size; i++)
    {
        ControlPointStruct updatePoint;
        int address = body.mid(index,2).toHex().toInt(0,16);
        index += 2;
        int value = body.mid(index,2).toHex().toInt(0,16);
        index += 2;

        updatePoint.setAddress(address);
        updatePoint.setValue(value);
        m_updatePointList.append(updatePoint);
    }
}
QList<ControlPointStruct> UpdatePointCmd::updatePointList() const
{
    return m_updatePointList;
}

void UpdatePointCmd::setUpdatePointList(const QList<ControlPointStruct> &updatePointList)
{
    m_updatePointList = updatePointList;
}

